<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>运输成本核算</title>
    <link rel="stylesheet" href="../../static/kj/element-ui/theme-chalk/index.css">
    <link rel="stylesheet" href="../../static/sa.css">
    <script src="../../static/kj/vue.min.js"></script>
    <script src="../../static/kj/element-ui/index.js"></script>
    <script src="../../static/sa.js"></script>
</head>
<body>
    <div id="app">
        <div class="c-panel f-main">
            <div class="c-title">运输成本核算</div>
            
            <!-- 成本统计概览 -->
            <div class="cost-overview">
                <el-row :gutter="20">
                    <el-col :span="6">
                        <el-card class="cost-card fuel">
                            <div class="cost-item">
                                <div class="cost-number">{{ costStats.fuel.toLocaleString() }}</div>
                                <div class="cost-label">燃油费用(元)</div>
                                <div class="cost-percent">{{ costStats.fuelPercent }}%</div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="6">
                        <el-card class="cost-card toll">
                            <div class="cost-item">
                                <div class="cost-number">{{ costStats.toll.toLocaleString() }}</div>
                                <div class="cost-label">过路费(元)</div>
                                <div class="cost-percent">{{ costStats.tollPercent }}%</div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="6">
                        <el-card class="cost-card labor">
                            <div class="cost-item">
                                <div class="cost-number">{{ costStats.labor.toLocaleString() }}</div>
                                <div class="cost-label">人工费用(元)</div>
                                <div class="cost-percent">{{ costStats.laborPercent }}%</div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="6">
                        <el-card class="cost-card total">
                            <div class="cost-item">
                                <div class="cost-number">{{ costStats.total.toLocaleString() }}</div>
                                <div class="cost-label">总成本(元)</div>
                                <div class="cost-percent">100%</div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>
            </div>

            <!-- 查询条件 -->
            <div class="query-section">
                <el-row :gutter="20">
                    <el-col :span="6">
                        <el-input v-model="queryForm.waybillNo" placeholder="运单号" clearable></el-input>
                    </el-col>
                    <el-col :span="6">
                        <el-select v-model="queryForm.vehicle" placeholder="选择车辆" clearable>
                            <el-option v-for="vehicle in vehicleList" :key="vehicle.id" :label="vehicle.name" :value="vehicle.id"></el-option>
                        </el-select>
                    </el-col>
                    <el-col :span="6">
                        <el-date-picker
                            v-model="queryForm.dateRange"
                            type="daterange"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            value-format="yyyy-MM-dd">
                        </el-date-picker>
                    </el-col>
                    <el-col :span="6">
                        <el-button type="primary" @click="searchCosts">查询</el-button>
                        <el-button @click="resetQuery">重置</el-button>
                        <el-button type="success" @click="calculateCost">成本计算</el-button>
                    </el-col>
                </el-row>
            </div>

            <!-- 成本明细列表 -->
            <div class="cost-detail-list">
                <el-table :data="costDetailData" border style="width: 100%" :summary-method="getSummaries" show-summary>
                    <el-table-column prop="waybillNo" label="运单号" width="120"></el-table-column>
                    <el-table-column prop="vehicle" label="车辆信息" width="150"></el-table-column>
                    <el-table-column prop="route" label="运输路线" width="200"></el-table-column>
                    <el-table-column prop="distance" label="行驶里程(km)" width="120"></el-table-column>
                    <el-table-column prop="fuelCost" label="燃油费(元)" width="100">
                        <template slot-scope="scope">
                            <span style="color: #E6A23C">{{ scope.row.fuelCost }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="tollCost" label="过路费(元)" width="100">
                        <template slot-scope="scope">
                            <span style="color: #909399">{{ scope.row.tollCost }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="laborCost" label="人工费(元)" width="100">
                        <template slot-scope="scope">
                            <span style="color: #67C23A">{{ scope.row.laborCost }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="otherCost" label="其他费用(元)" width="120">
                        <template slot-scope="scope">
                            <span style="color: #F56C6C">{{ scope.row.otherCost }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="totalCost" label="总成本(元)" width="120">
                        <template slot-scope="scope">
                            <span style="font-weight: bold; color: #409EFF">{{ scope.row.totalCost }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="income" label="运输收入(元)" width="120">
                        <template slot-scope="scope">
                            <span style="color: #67C23A">{{ scope.row.income }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="profit" label="利润(元)" width="100">
                        <template slot-scope="scope">
                            <span :style="{color: scope.row.profit >= 0 ? '#67C23A' : '#F56C6C', fontWeight: 'bold'}">
                                {{ scope.row.profit }}
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="150">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="viewCostDetail(scope.row)">详情</el-button>
                            <el-button size="mini" type="primary" @click="editCost(scope.row)">编辑</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>

            <!-- 分页 -->
            <div class="pagination-wrapper">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[10, 20, 50]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
            </div>
        </div>

        <!-- 成本详情对话框 -->
        <el-dialog title="成本明细详情" :visible.sync="detailDialogVisible" width="70%">
            <div v-if="selectedCost">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <div class="detail-section">
                            <h4>基本信息</h4>
                            <p><strong>运单号：</strong>{{ selectedCost.waybillNo }}</p>
                            <p><strong>车辆信息：</strong>{{ selectedCost.vehicle }}</p>
                            <p><strong>运输路线：</strong>{{ selectedCost.route }}</p>
                            <p><strong>行驶里程：</strong>{{ selectedCost.distance }}公里</p>
                            <p><strong>运输时间：</strong>{{ selectedCost.transportTime }}小时</p>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <div class="detail-section">
                            <h4>成本明细</h4>
                            <p><strong>燃油费用：</strong>{{ selectedCost.fuelCost }}元 ({{ selectedCost.fuelConsumption }}L × {{ selectedCost.fuelPrice }}元/L)</p>
                            <p><strong>过路费：</strong>{{ selectedCost.tollCost }}元</p>
                            <p><strong>司机工资：</strong>{{ selectedCost.driverSalary }}元</p>
                            <p><strong>装卸费：</strong>{{ selectedCost.loadingCost }}元</p>
                            <p><strong>保险费：</strong>{{ selectedCost.insuranceCost }}元</p>
                        </div>
                    </el-col>
                </el-row>
                
                <div class="detail-section">
                    <h4>其他费用明细</h4>
                    <el-table :data="selectedCost.otherCostDetails" border size="mini">
                        <el-table-column prop="type" label="费用类型" width="120"></el-table-column>
                        <el-table-column prop="description" label="费用说明" width="200"></el-table-column>
                        <el-table-column prop="amount" label="金额(元)" width="100"></el-table-column>
                        <el-table-column prop="remark" label="备注"></el-table-column>
                    </el-table>
                </div>
                
                <div class="detail-section">
                    <h4>成本分析</h4>
                    <div class="cost-analysis">
                        <div class="analysis-item">
                            <span class="label">总成本：</span>
                            <span class="value total">{{ selectedCost.totalCost }}元</span>
                        </div>
                        <div class="analysis-item">
                            <span class="label">运输收入：</span>
                            <span class="value income">{{ selectedCost.income }}元</span>
                        </div>
                        <div class="analysis-item">
                            <span class="label">利润：</span>
                            <span :class="['value', selectedCost.profit >= 0 ? 'profit' : 'loss']">{{ selectedCost.profit }}元</span>
                        </div>
                        <div class="analysis-item">
                            <span class="label">利润率：</span>
                            <span :class="['value', selectedCost.profitRate >= 0 ? 'profit' : 'loss']">{{ selectedCost.profitRate }}%</span>
                        </div>
                    </div>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="detailDialogVisible = false">关闭</el-button>
                <el-button type="primary" @click="exportCostDetail">导出详情</el-button>
            </span>
        </el-dialog>

        <!-- 成本计算器对话框 -->
        <el-dialog title="智能成本计算器" :visible.sync="calculatorDialogVisible" width="60%">
            <el-form :model="calculatorForm" :rules="calculatorRules" ref="calculatorForm" label-width="120px">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="运输距离" prop="distance">
                            <el-input v-model.number="calculatorForm.distance" placeholder="公里">
                                <template slot="append">公里</template>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="车辆类型" prop="vehicleType">
                            <el-select v-model="calculatorForm.vehicleType" placeholder="选择车辆类型" @change="updateFuelConsumption">
                                <el-option label="小型冷藏车(5吨)" value="small" :data-consumption="12"></el-option>
                                <el-option label="中型冷藏车(10吨)" value="medium" :data-consumption="18"></el-option>
                                <el-option label="大型冷藏车(20吨)" value="large" :data-consumption="25"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="燃油价格" prop="fuelPrice">
                            <el-input v-model.number="calculatorForm.fuelPrice" placeholder="元/升">
                                <template slot="append">元/升</template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="运输天数" prop="days">
                            <el-input v-model.number="calculatorForm.days" placeholder="天数">
                                <template slot="append">天</template>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="司机人数" prop="driverCount">
                            <el-input v-model.number="calculatorForm.driverCount" placeholder="人数">
                                <template slot="append">人</template>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="日工资标准" prop="dailyWage">
                            <el-input v-model.number="calculatorForm.dailyWage" placeholder="元/天">
                                <template slot="append">元/天</template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                
                <div class="calculation-result" v-if="calculationResult">
                    <h4>计算结果</h4>
                    <el-row :gutter="20">
                        <el-col :span="6">
                            <div class="result-item">
                                <div class="result-label">燃油费</div>
                                <div class="result-value">{{ calculationResult.fuelCost }}元</div>
                            </div>
                        </el-col>
                        <el-col :span="6">
                            <div class="result-item">
                                <div class="result-label">过路费</div>
                                <div class="result-value">{{ calculationResult.tollCost }}元</div>
                            </div>
                        </el-col>
                        <el-col :span="6">
                            <div class="result-item">
                                <div class="result-label">人工费</div>
                                <div class="result-value">{{ calculationResult.laborCost }}元</div>
                            </div>
                        </el-col>
                        <el-col :span="6">
                            <div class="result-item">
                                <div class="result-label">总成本</div>
                                <div class="result-value total">{{ calculationResult.totalCost }}元</div>
                            </div>
                        </el-col>
                    </el-row>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="calculatorDialogVisible = false">关闭</el-button>
                <el-button type="primary" @click="calculateTransportCost">计算成本</el-button>
                <el-button type="success" @click="saveCalculation">保存结果</el-button>
            </span>
        </el-dialog>
    </div>

    <script>
        var app = new Vue({
            el: '#app',
            data() {
                return {
                    costStats: {
                        fuel: 98600,
                        fuelPercent: 42,
                        toll: 45200,
                        tollPercent: 19,
                        labor: 68500,
                        laborPercent: 29,
                        total: 235800
                    },
                    queryForm: {
                        waybillNo: '',
                        vehicle: '',
                        dateRange: []
                    },
                    vehicleList: [
                        {id: 'CRC001', name: '东风天锦冷藏车(湘D88888)'},
                        {id: 'CRC002', name: '解放虎V冷藏车(湘D66666)'},
                        {id: 'CC001', name: '衡阳湘莲专用冷藏车(湘D99999)'}
                    ],
                    costDetailData: [
                        {
                            waybillNo: 'HY2024001',
                            vehicle: '东风天锦冷藏车(湘D88888)',
                            route: '衡阳→长沙',
                            distance: 160,
                            fuelCost: 256,
                            tollCost: 64,
                            laborCost: 300,
                            otherCost: 80,
                            totalCost: 700,
                            income: 1200,
                            profit: 500,
                            transportTime: 3,
                            fuelConsumption: 25.6,
                            fuelPrice: 10,
                            driverSalary: 300,
                            loadingCost: 50,
                            insuranceCost: 30,
                            profitRate: 41.7,
                            otherCostDetails: [
                                {type: '停车费', description: '衡阳服务区停车', amount: 20, remark: '休息停车'},
                                {type: '餐费', description: '司机餐费补贴', amount: 40, remark: '午餐费用'},
                                {type: '冰块费', description: '湘莲保鲜冰块', amount: 20, remark: '保鲜材料'}
                            ]
                        },
                        {
                            waybillNo: 'HY2024002',
                            vehicle: '解放虎V冷藏车(湘D66666)',
                            route: '衡阳祁东→衡阳市区',
                            distance: 35,
                            fuelCost: 63,
                            tollCost: 0,
                            laborCost: 150,
                            otherCost: 37,
                            totalCost: 250,
                            income: 380,
                            profit: 130,
                            transportTime: 1,
                            fuelConsumption: 6.3,
                            fuelPrice: 10,
                            driverSalary: 150,
                            loadingCost: 25,
                            insuranceCost: 12,
                            profitRate: 34.2,
                            otherCostDetails: [
                                {type: '装卸费', description: '黄花菜装卸', amount: 25, remark: '人工装卸'},
                                {type: '通讯费', description: '司机通讯费', amount: 12, remark: '电话费补贴'}
                            ]
                        }
                    ],
                    currentPage: 1,
                    pageSize: 10,
                    total: 2,
                    detailDialogVisible: false,
                    calculatorDialogVisible: false,
                    selectedCost: null,
                    calculatorForm: {
                        distance: '',
                        vehicleType: '',
                        fuelPrice: 10,
                        days: 1,
                        driverCount: 1,
                        dailyWage: 300
                    },
                    calculatorRules: {
                        distance: [
                            { required: true, message: '请输入运输距离', trigger: 'blur' },
                            { type: 'number', min: 1, message: '距离必须大于0', trigger: 'blur' }
                        ],
                        vehicleType: [
                            { required: true, message: '请选择车辆类型', trigger: 'change' }
                        ]
                    },
                    calculationResult: null
                }
            },
            methods: {
                searchCosts() {
                    this.$message.success('查询完成');
                },
                resetQuery() {
                    this.queryForm = {
                        waybillNo: '',
                        vehicle: '',
                        dateRange: []
                    };
                },
                calculateCost() {
                    this.calculatorDialogVisible = true;
                },
                viewCostDetail(row) {
                    this.selectedCost = row;
                    this.detailDialogVisible = true;
                },
                editCost(row) {
                    this.$message.info('编辑功能开发中');
                },
                exportCostDetail() {
                    this.$message.success('成本详情导出成功');
                },
                updateFuelConsumption(value) {
                    const consumptionMap = {
                        'small': 12,
                        'medium': 18,
                        'large': 25
                    };
                    this.fuelConsumption = consumptionMap[value] || 15;
                },
                calculateTransportCost() {
                    this.$refs.calculatorForm.validate((valid) => {
                        if (valid) {
                            const { distance, vehicleType, fuelPrice, days, driverCount, dailyWage } = this.calculatorForm;
                            
                            // 油耗计算（百公里）
                            const consumptionMap = {
                                'small': 12,
                                'medium': 18,
                                'large': 25
                            };
                            const consumption = consumptionMap[vehicleType];
                            const fuelCost = Math.round((distance / 100) * consumption * fuelPrice);
                            
                            // 过路费计算（按每公里0.4元）
                            const tollCost = Math.round(distance * 0.4);
                            
                            // 人工费计算
                            const laborCost = days * driverCount * dailyWage;
                            
                            // 其他费用（按总成本的8%估算）
                            const otherCost = Math.round((fuelCost + tollCost + laborCost) * 0.08);
                            
                            this.calculationResult = {
                                fuelCost,
                                tollCost,
                                laborCost,
                                otherCost,
                                totalCost: fuelCost + tollCost + laborCost + otherCost
                            };
                        }
                    });
                },
                saveCalculation() {
                    if (this.calculationResult) {
                        this.$message.success('计算结果已保存');
                        this.calculatorDialogVisible = false;
                    }
                },
                getSummaries(param) {
                    const { columns, data } = param;
                    const sums = [];
                    columns.forEach((column, index) => {
                        if (index === 0) {
                            sums[index] = '合计';
                            return;
                        }
                        if (['fuelCost', 'tollCost', 'laborCost', 'otherCost', 'totalCost', 'income', 'profit'].includes(column.property)) {
                            const values = data.map(item => Number(item[column.property]));
                            if (!values.every(value => isNaN(value))) {
                                sums[index] = values.reduce((prev, curr) => {
                                    const value = Number(curr);
                                    if (!isNaN(value)) {
                                        return prev + curr;
                                    } else {
                                        return prev;
                                    }
                                }, 0);
                                sums[index] += '';
                            } else {
                                sums[index] = '';
                            }
                        } else {
                            sums[index] = '';
                        }
                    });
                    return sums;
                },
                handleSizeChange(val) {
                    this.pageSize = val;
                },
                handleCurrentChange(val) {
                    this.currentPage = val;
                }
            }
        });
    </script>

    <style scoped>
        .cost-overview {
            margin-bottom: 30px;
        }
        
        .cost-card {
            text-align: center;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .cost-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }
        
        .cost-card.fuel {
            border-left: 4px solid #E6A23C;
        }
        
        .cost-card.toll {
            border-left: 4px solid #909399;
        }
        
        .cost-card.labor {
            border-left: 4px solid #67C23A;
        }
        
        .cost-card.total {
            border-left: 4px solid #409EFF;
        }
        
        .cost-number {
            font-size: 24px;
            font-weight: bold;
            color: #303133;
        }
        
        .cost-label {
            color: #606266;
            margin: 5px 0;
        }
        
        .cost-percent {
            font-size: 12px;
            color: #909399;
        }
        
        .query-section {
            background: #f5f5f5;
            padding: 20px;
            margin-bottom: 20px;
            border-radius: 4px;
        }
        
        .cost-detail-list {
            margin-bottom: 20px;
        }
        
        .pagination-wrapper {
            text-align: center;
            padding: 20px 0;
        }
        
        .detail-section {
            margin-bottom: 20px;
        }
        
        .detail-section h4 {
            color: #409EFF;
            border-bottom: 2px solid #409EFF;
            padding-bottom: 10px;
            margin-bottom: 15px;
        }
        
        .detail-section p {
            margin: 10px 0;
            line-height: 1.6;
        }
        
        .cost-analysis {
            display: flex;
            justify-content: space-around;
            background: #f8f9fa;
            padding: 20px;
            border-radius: 4px;
        }
        
        .analysis-item {
            text-align: center;
        }
        
        .analysis-item .label {
            display: block;
            color: #666;
            margin-bottom: 10px;
        }
        
        .analysis-item .value {
            display: block;
            font-size: 18px;
            font-weight: bold;
        }
        
        .analysis-item .value.total {
            color: #409EFF;
        }
        
        .analysis-item .value.income {
            color: #67C23A;
        }
        
        .analysis-item .value.profit {
            color: #67C23A;
        }
        
        .analysis-item .value.loss {
            color: #F56C6C;
        }
        
        .calculation-result {
            margin-top: 20px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 4px;
        }
        
        .calculation-result h4 {
            color: #409EFF;
            margin-bottom: 20px;
        }
        
        .result-item {
            text-align: center;
            background: white;
            padding: 15px;
            border-radius: 4px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        .result-label {
            color: #666;
            font-size: 14px;
            margin-bottom: 10px;
        }
        
        .result-value {
            font-size: 18px;
            font-weight: bold;
            color: #409EFF;
        }
        
        .result-value.total {
            color: #E6A23C;
            font-size: 20px;
        }
    </style>
</body>
</html>